package com.itheima.tanhua.dubbo.service.impl;

import com.itheima.tanhua.dubbo.mappers.UsersMapper;
import com.itheima.tanhua.dubbo.service.UsersService;
import com.itheima.tanhua.model.dto.BlackListDeleteDto;
import com.itheima.tanhua.model.vo.BlackListItemVo;
import com.itheima.tanhua.model.vo.BlackListVo;
import com.itheima.tanhua.model.vo.CommonSettingsVo;
import com.itheima.tanhua.model.dto.QuestionsDto;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author H Monster
 */
@DubboService
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper myMapper;

    @Override
    public CommonSettingsVo selectCommonSettings(Integer id) {
        return myMapper.selectCommonSettings(id);
    }

    @Override
    public QuestionsDto selectContent(String userId) {
        return myMapper.selectContent(userId);
    }

    @Override
    public void updateQuestions(QuestionsDto questionsDto) {
        myMapper.updateQuestions(questionsDto);
    }

    @Override
    public void insertQuestions(QuestionsDto questionsDto) {
        myMapper.insertQuestions(questionsDto);
    }

    @Override
    public void insertNotifications(CommonSettingsVo commonSettingsVo) {
        myMapper.insertNotifications(commonSettingsVo);
    }

    @Override
    public void deleteBlacklist(BlackListDeleteDto blackListDto) {
        myMapper.deleteBlacklist(blackListDto);
    }

    @Override
    public BlackListVo selectBlackList(Integer page, Integer pageSize, String userId) {
        BlackListVo vo = new BlackListVo();
        // 组装分页条件
        // limit(页码-1) * 每页显示条数, 每页显示条数
        Integer skip = (page - 1) * pageSize;

        // 查询分页数据
        List<BlackListItemVo> blackList = myMapper.getBlackList(skip, pageSize, userId);

        // 查询满足条件的总记录数
        Long counts = myMapper.countBlackList(userId);

        // 计算页数
        Long totalPages = counts / pageSize;
        if (counts % pageSize != 0) {
            totalPages++;
        }

        // 组装并返回
        vo.setCount(counts.intValue());
        vo.setPages(totalPages.intValue());
        vo.setItems(blackList);
        vo.setPage(page);
        vo.setPagesize(pageSize);

        return vo;
    }
}
